package com.medical.medical.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.medical.medical.entity.Notice;
import com.medical.medical.mapper.NoticeMapper;
import com.medical.medical.util.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName NoticeServiceImpl
 * @Description 坤坤最帅 - 坤坤最美!
 * @Author QinXu
 * @Date 2022/9/14 10:09
 * @Version 1.0
 */
@Slf4j
@Service
public class NoticeServiceImpl implements NoticeService{
    @Resource
    private NoticeMapper noticeMapper;

   

    @Override
     /**
      *@author WangLuDong
      *@Description 方法描述 查询所有公告
      *@Date 2022-09-16 09:46:15
       * @param
      *@Return {@link List< Notice>}
      */
    public PageInfo<Notice> getAllNotice(Integer pageNum){
        PageHelper.startPage(pageNum,2);
        List<Notice> allNotice = noticeMapper.getAllNotice();
        PageInfo<Notice> noticePageInfo = new PageInfo<>(allNotice);
        return noticePageInfo;
    }
    /**
      * @description 根据条件查询系统更新公告条数
      * @author GUOYN
      * @date 2022-09-15 09:25:07  * @param startTime
     * @param terminationTime
      * @return {@link int}
      */
    @Override
    public int getNoticeCount(Date startTime, Date terminationTime,int noticeType) {
        try {
            return noticeMapper.getNoticeCount(startTime,terminationTime,noticeType);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("根据条件查询系统更新公告条数时发生异常！");
        }
        return 0;
    }
    /**
      * @description 根据条件查询系统更新公告
      * @author GUOYN
      * @date 2022-09-15 09:33:16  * @param startTime
     * @param terminationTime
     * @param noticeType
     * @param current
      * @return {@link List< Notice>}
      */
    @Override
    public List<Notice> getSystemNoticeList(Date startTime,
                                            Date terminationTime,
                                            int noticeType,
                                            int current) {
        int start = (current - 1) * Constants.PAGE_SIZE;
        try {
            return noticeMapper.getSystemNoticeList(startTime,terminationTime,
                                                    noticeType,start,Constants.PAGE_SIZE);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("根据条件查询系统更新公告时发生异常！");
        }
        return null;
    }
    /**
      * @description 根据id获得当前系统更新公告
      * @author GUOYN
      * @date 2022-09-15 12:16:03  * @param id
      * @return {@link Notice}
      */
    @Override
    public Notice getSystemNoticeView(int id,int noticeType) {
        try {
            return noticeMapper.getSystemNoticeView(id,noticeType);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("根据id获得当前系统更新公告时发生异常！");
        }
        return null;
    }
    /**
      * @description 添加系统更新公告
      * @author GUOYN
      * @date 2022-09-15 14:44:06  * @param notice
      * @return {@link boolean}
      */
    @Override
    public boolean addSystemNotice(Notice notice) {
        try {
            int flag = noticeMapper.addSystemNotice(notice);
            if (flag != -1){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("添加系统更新公告时发生异常！");
        }
        return false;
    }
    /**
     * @description 修改系统更新公告（发布状态/发布时间）
     * @author GUOYN
     * @date 2022-09-15 15:19:40  * @param notice
     * @return {@link boolean}
     */
    @Override
    public boolean updateSystemNotice(Notice notice) {
        try {
            int flag = noticeMapper.updateSystemNotice(notice);
            if (flag != -1){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("修改系统更新公告（发布状态/发布时间）时发生异常！");
        }
        return false;
    }

    /**
     * @author qinxu
     * @description 公告后台管理,根据公告标题名称模糊查询公告
     * @param pageNum
     * @param noticeName
     * @return
     */
    @Override
    public PageInfo<Notice> getNoticeByNoticeName(Integer pageNum, String noticeName) {
        PageHelper.startPage(pageNum,2);
        List<Notice> noticeList = noticeMapper.getNoticeByNoticeName(noticeName);
        PageInfo<Notice> noticePageInfo = new PageInfo<>(noticeList);
        return noticePageInfo;
    }

    /**
     * 公告后台管理
     * 根据id删除公告
     * qinxu
     * @param noticeId
     * @return
     */
    @Override
    public int deleteById(String noticeId) {
        int result = noticeMapper.deleteById(noticeId);
        return result;
    }
    /*
     * @author: 添加公告
     * @description:
     * No such property: code for class: Script1
     * @return:
     * @time: 2022/9/24 12:06
     */
    @Override
    public int addNotice(Notice notice) {
       int result =  noticeMapper.addNotice(notice);
        return 0;
    }
    /**
     * @description 查询最新公告信息
     * @author GUOYN
     * @date 2022-09-26 11:19:51  * @param
     * @return {@link Notice}
     */
    @Override
    public Notice getNewSystemNoticeView() {
        try {
            return noticeMapper.getNewSystemNoticeView();
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询最新公告信息时发生异常！");
        }
        return null;
    }
}
